/*
 *
 * 名称：九宫
 * 功能：九宫运转逻辑
 * 作者：清河张
 * 时间：2025.05.15
 *
 */
#include "gong9.h"

//保存9宫
static XZGONG _gongwei9[9] =
{ //号  序  先 后  河数 河数   五行
    0, 4, 1, 3, 2,  7, WUXING5::HUOO, 0,
    1, 0, 8, 6, 1,  6, WUXING5::SHUI, 0,
    2, 5, 5, 8, 5, 10, WUXING5::TUUU, 0,
    3, 2, 3, 4, 3,  8, WUXING5::MUUU, 0,
    4, 3, 2, 5, 3,  8, WUXING5::MUUU, 0,
    5, 8, 0, 0, 5, 10, WUXING5::TUUU, 0,
    6, 7, 7, 1, 4,  9, WUXING5::JINN, 0,
    7, 6, 6, 2, 4,  9, WUXING5::JINN, 0,
    8, 1, 4, 7, 5, 10, WUXING5::TUUU, 0,
};
//快速索引
static XZGONG* _gua1p[9] =
{
    &_gongwei9[5],
    &_gongwei9[0],
    &_gongwei9[4],
    &_gongwei9[3],
    &_gongwei9[8],
    &_gongwei9[2],
    &_gongwei9[7],
    &_gongwei9[6],
    &_gongwei9[1],
};

static XZGONG* _gua2p[9] =
{
    &_gongwei9[5],
    &_gongwei9[6],
    &_gongwei9[7],
    &_gongwei9[0],
    &_gongwei9[3],
    &_gongwei9[4],
    &_gongwei9[1],
    &_gongwei9[8],
    &_gongwei9[2],
};

static XZGONG*  _gongxup[9] =
{
    &_gongwei9[1],
    &_gongwei9[8],
    &_gongwei9[3],
    &_gongwei9[4],
    &_gongwei9[0],
    &_gongwei9[2],
    &_gongwei9[7],
    &_gongwei9[6],
    &_gongwei9[5],
};

static int fei(int gong, int jb)//g宫飞jb次的新宫
{
    return XUNHUAN ( gong + jb, 9 );
}

static int zhuan(int gong, int jd)//g宫飞jd次的新宫
{
    int x = _gongwei9[gong%9]._xu;
    return _gongxup[XUNHUAN( x + jd, 8 )]->_id ;
}

static int xiantian(int gong)//g宫先天卦就应宫
{
    int gua2 = _gongwei9[gong%9]._gua2;
    return _gua1p[gua2]->_id;
}

static int houtian(int gong)//g宫后天卦就应宫
{
    int gua1 = _gongwei9[gong%9]._gua1;
    return _gua2p[gua1]->_id;
}

int XZGONG::gid() const
{
    return _id > 0 ? _id : 9;
}

int XZGONG::xshu() const
{
    int a,b;
    a = _id < _gua2 ? _id : _gua2;
    b = _ht1 < _ht2 ? _ht1 : _ht2;
    return a < b ? a : b;
}

int XZGONG::dshu() const
{
    int a,b;
    a = _id > _gua2 ? _id : _gua2;
    b = _ht1 > _ht2 ? _ht1 : _ht2;
    return a > b ? a : b;
}

WUXING5 XZGONG::wx5() const
{
    return _wx;
}

int XZGONG::izhi1() const
{
    if(_xu > 7) return -1;
    return _xu % 2 ? _xu + _xu / 2 : _xu / 2 * 3;
}

int XZGONG::izhi2() const
{
    if(_xu > 7) return -1;
    return _xu % 2 ? _xu + _xu / 2 + 1 : -1 ;
}

ZHI12 XZGONG::zhi1() const
{
    return izhi1();
}

ZHI12 XZGONG::zhi2() const
{
    return izhi2();
}

int XZGONG::jd(const XZGONG *g) const
{
    return g->_xu - _xu;
}

int XZGONG::jb(const XZGONG *g) const
{
    return g->_id - _id;
}

GUA8 XZGONG::gua1() const
{
    return _gua1;
}

GUA8 XZGONG::gua2() const
{
    return _gua2;
}


bool XZGONG::zhg_hu() const
{
    return 5 == _id;
}

int XZGONG::fei(int jb)
{
    return ::fei(_id, jb);
}

int XZGONG::zhuan(int jd)
{
    return ::zhuan(_id, jd);
}

int XZGONG::duimian()
{
    return 10 - _id;
}

int XZGONG::xiantian()
{
    return ::xiantian(_id);
}

int XZGONG::houtian()
{
    return ::houtian(_id);
}


int gong9_zhi_gid(int z)
{
    return gong9_zhi_gong(z)->_id;
}

XZGONG *gong9_zhi_gong(int z)
{
    XZGONG* gong = &_gongwei9[5];
    for( int i = 0; i < 9; ++i)
    {
        if(z == _gongwei9[i].izhi1() ||
            z == _gongwei9[i].izhi2())
        {
            gong = &_gongwei9[i];
            break;
        }
    }

    return gong;
}


int gong9_zhg_gid()
{
    return 5;
}

XZGONG *gong9_zhg_gong()
{
    return &_gongwei9[gong9_zhg_gid()];
}

int gong9_xu_gid(int x)
{
    return gong9_xu_gong(x)->_id;
}

XZGONG *gong9_xu_gong(int x)
{
    if(x > 8) return gong9_zhg_gong();
    return _gongxup[x%9];
}

int gong9_gua1_gid(int g)
{
    return gong9_gua1_gong(g)->_id;
}

XZGONG *gong9_gua1_gong(int g)
{
    if(g > 8) return gong9_zhg_gong();
    return _gua1p[g];
}

int gong9_gua2_gid(int g)
{
    return gong9_gua2_gong(g)->_id;
}

XZGONG *gong9_gua2_gong(int g)
{
    if(g > 8) return gong9_zhg_gong();
    return _gua2p[g];
}

XZGONG *gong9_gid_gong(int gid)
{
    return &_gongwei9[gid%9];
}

